//错误处理
const formatRsponse = require("./responseFormat");
class ServiceErr extends Error {
  /**
   *
   * @param {*} message //错误的消息
   * @param {*} code //错误消息码
   */
  constructor(message, code) {
    super(message);
    this.code = code;
  }

  toResponseJSON() {
    return formatRsponse(this.code, this.message, null);
  }
}

//文件上传错误
exports.UploadError = class extends ServiceErr {
  constructor(message) {
    super(message, 413);
  }
};
// 禁止访问错误
exports.ForbiddenError = class extends ServiceErr {
  constructor(message) {
    super(message, 401);
  }
};

//验证错误
exports.ValidationError = class extends ServiceErr {
  constructor(message) {
    super(message, 401);
  }
};

//无资源错误
exports.NotFoundError = class extends ServiceErr {
  constructor() {
    super("not found", 404);
  }
};

//未知错误
exports.UnknownError = class extends ServiceErr {
  constructor() {
    super("server internal err", 500);
  }
};

exports.ServiceErr = ServiceErr;
